package cn.ysym.dao;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import cn.ysym.model.Product;

/**
 * Created by Administrator on 2016/12/19 0019.
 */

public class ProductDao extends DaoBase {

    public ProductDao(Context context) {
        super(context);
    }

    public Product queryById(String productId) {
        List<String[]> objs = this.query(Integer.MAX_VALUE, "select productId, productName, productNamePy from product where productId = ?", productId);
        if (objs.size() > 0) {
            String[] row = objs.get(0);
            Product product = new Product();
            product.setProductId(row[0]);
            product.setProductName(row[1]);
            product.setProductNamePy(row[2]);
            return product;
        }

        return null;
    }

    public List<Product> query(String filterStr) {
        List<Product> ret = new ArrayList<>();
        List<String[]> objs;
        if (filterStr != null && filterStr.length() > 0)
            objs = this.query(Integer.MAX_VALUE, "select productId, productName, productNamePy from product where productId like '%" + filterStr + "%' or productName like '%" + filterStr + "%' or productNamePy like '%" + filterStr + "%' order by productName");
        else
            objs = this.query(Integer.MAX_VALUE, "select productId, productName, productNamePy from product order by productName");
        for (String[] row : objs) {
            Product product = new Product();
            product.setProductId(row[0]);
            product.setProductName(row[1]);
            product.setProductNamePy(row[2]);
            ret.add(product);
        }
        return ret;
    }

    public void insert(Product model) {
        this.execSql("insert into product(productId, productName, productNamePy) values(?, ?, ?)", model.getProductId(), model.getProductName(), model.getProductNamePy());
    }

    public void clearAll() {
        this.execSql("delete from product");
    }
}
